<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>会员支付</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body class="layui-anim layui-anim-up">
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md14 x-so" onsubmit="return false;">
            <div class="layui-inline">
                <lable class="layui-form-label" style="margin-top: 14px">电话号码：</lable>
                <input type="text" name="keywordPhone" id="keywordPhone"
                       placeholder="请输入电话号码" autocomplete="off" maxlength="11" style="width: 170px" class="layui-input"
                       lay-verify="keywordPhone">
            </div>
            <button class="layui-btn" id="btn-seach"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <div class="layui-inline" id="memberInfo" STYLE="text-align: center">
        <div class="layui-input-inline">
            <input type="hidden" class="layui-input">
        </div>
    </div>
    <form class="layui-form" id="rechargeForm" onsubmit="return false">
        <input type="hidden" id="workerId" name="workerId" value="${worker.workerId}">
        <input type="hidden" id="orderId" name="orderId" value="${param.orderId}">
        <input type="hidden" id="memberId" name="memberId">
        <div class="layui-form-item">
            <label for="memberName" class="layui-form-label">
                会员名：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="memberName" name="memberName" required=""
                       autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="memberRoleName" class="layui-form-label">
                会员类型：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="memberRoleName" name="memberRoleName" required=""
                       autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="memberMoney" class="layui-form-label">
                余额：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="memberMoney" name="memberMoney" required=""
                       autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="memberCount" class="layui-form-label">
                积分：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="memberCount" name="memberCount" required=""
                       autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="orderMoney" class="layui-form-label">
                订单金额：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="orderMoney" name="orderMoney" required="" readonly
                       autocomplete="off" class="layui-input" value="${param.orderMoney}">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="payMoney" class="layui-form-label">
                实际支付：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="payMoney" name="payMoney" required="" readonly
                       autocomplete="off" class="layui-input" STYLE="color: red">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <button class="layui-btn" id="normalPay"
                    onclick="location.replace('${pageContext.request.contextPath}/foreground?m=normalPayPage&orderId='+$('#orderId').val()+'&orderMoney='+$('#orderMoney').val())">
                非会员支付
            </button>
            <%--<button type="reset" class="layui-btn layui-btn-primary" onclick="$('#keywordPhone').val('')">重置</button>--%>
            <button type="reset" id="reset" class="layui-btn layui-btn-primary" >重置</button>
        </div>
        <div id="normalPayPassword"></div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <a class="layui-btn layui-btn-normal" id="memberNormalPay" style="display: none;"
               onclick="showNoBalancePasswordPart()">会员非余额支付</a>
            <button class="layui-btn" id="memberNormalPayBtn" lay-filter="normalpay" lay-submit=""
                    STYLE="display: none">确认支付
            </button>
        </div>
        <div id="memberPayPassword"></div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <a class="layui-btn layui-btn-normal" id="memberPay" style="display: none;"
               onclick="showBalancePasswordPart()">使用余额支付</a>
            <button class="layui-btn" id="memberPayBtn" lay-filter="pay" lay-submit="" STYLE="display: none">确认支付
            </button>
        </div>


    </form>
</div>
<script>

    var isNoBalance = false;
    function showNoBalancePasswordPart() {
        if (!isNoBalance) {
            $("#normalPayPassword").append('<div class="layui-form-item" disabled="disabled">\n' +
                '                <label for="memberPassword" class="layui-form-label">\n' +
                '                    密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="memberPassword" name="memberPassword" value="" lay-verify="password"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>\n')
            $("#memberNormalPay").text("不使用会员非余额支付");
            $("#memberNormalPayBtn").show();
            isNoBalance = true;
        } else {
            $("#normalPayPassword").empty();
            $("#memberNormalPayBtn").hide();
            $("#memberNormalPay").text("使用会员非余额支付");
            isNoBalance = false;
        }
    };

    var isBalance = false;
    function showBalancePasswordPart() {
        if (!isBalance) {
            $("#memberPayPassword").append('<div class="layui-form-item" disabled="disabled">\n' +
                '                <label for="memberPassword" class="layui-form-label">\n' +
                '                    密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="memberPassword" name="memberPassword" value="" lay-verify="password"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>\n')
            $("#memberPay").text("不使用余额支付");
            $("#memberPayBtn").show();
            isBalance = true;
        } else {
            $("#memberPayPassword").empty();
            $("#memberPayBtn").hide();
            $("#memberPay").text("使用余额支付");
            isBalance = false;
        }
    };


    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            rechargeMoney: [/^([1-9]\d*|0)(\.\d{1,2})?$/, '充值金额不能为空且最多保留两位小数！'],
            keywordPhone:[/^1[0-9]{10}/,'请输入正确的电话号码']
        });

        //监听提交会员余额支付
        form.on('submit(pay)', function (data) {
            var memberId = $('#memberId').val();
            var memberMoney = $('#memberMoney').val();
            var payMoney = $('#payMoney').val();
            if (parseFloat(memberMoney) < parseFloat(payMoney)) {
                layer.msg("余额不足请充值！");
                return;
            }
            //发异步，把数据提交给java
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/foreground?m=memberPay",
                data: data.field,
                dataType: "json",
                success: function (data) {
                    layer.msg(data.msg, function () {
                            if (data.code === 200) {
                                window.parent.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                        }
                    });

                }
            });
            return false;
        });

        //监听提交会员非余额支付
        form.on('submit(normalpay)', function (data) {
            var memberId = $('#memberId').val();
            var payMoney = $('#payMoney').val();
            //发异步，把数据提交给java
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/foreground?m=memberNormalPay",
                data: data.field,
                dataType: "json",
                success: function (data) {
                    layer.msg(data.msg, function () {
                        if (data.code === 200) {
                            if (data.code === 200) {
                                window.parent.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);

                            }
                        }
                    });

                }
            });
            return false;
        });

        if ($('#memberId').val() != null && $('#memberId').val() != "") {
            changeValue();
        }

        function changeValue() {
            $("#memberName").empty();
            $("#memberRoleName").empty();
            $("#memberMoney").empty();
            $("#rechargeMoney").empty();
            $("#memberCount").empty();
            // 获取搜索的数据
            var memberPhone = $("input[name=keywordPhone]").val();
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/foreground?m=findMember",
                data: {memberPhone: memberPhone},
                dataType: "json",
                success: function (data) {
                    if (data.code === 200) {
                        if (data.data.memberId != null) {
                            $('#memberNormalPay').show();
                            $('#memberRechargeBtn').show();
                            $('#memberPay').show();
                            $("#memberName").val(data.data.memberName);
                            $("#memberId").val(data.data.memberId);
                            $("#memberRoleName").val(data.data.memberRole.memberRoleName + " (" + data.data.memberRole.memberRoleCount + "折)");
                            $("#memberMoney").val(data.data.memberMoney);
                            $("#memberCount").val(data.data.memberCount);
                            $("#payMoney").val(keepTwoDecimal($("#orderMoney").val() * data.data.memberRole.memberRoleCount / 10));
                        }
                    } else {
                        $('#memberNormalPay').hide();
                        $('#memberRechargeBtn').hide();
                        $('#memberPay').hide();
                        $("#memberPayBtn").hide();
                        layer.msg(data.msg);
                    }
                }
            });

        }

        function keepTwoDecimal(num) {
            var result = parseFloat(num);
            if (isNaN(result)) {
                alert('传递参数错误，请检查！');
                return false;
            }
            result = Math.round(num * 100) / 100;
            return result;
        }

        // 搜索
        $("#btn-seach").click(function () {
            changeValue();
        });
        $("#reset").click(function (){
            $('#keywordPhone').val('');
            $('#memberNormalPay').hide();
            $('#memberRechargeBtn').hide();
            $('#memberPay').hide();
            $("#memberPayBtn").hide();
        });

    });
</script>

</body>

</html>